package com.z3n.entities.controller;

import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import com.z3n.entities.Z3nGroupsUser;
import java.util.List;
import javax.persistence.Query;

@SuppressWarnings("unchecked")
@JPAManager(targetEntity = com.z3n.entities.Z3nGroupsUser.class)
public class Z3nGroupsUserManager {

	private EntityManagerFactory emf;

	public Z3nGroupsUserManager() {
	
	}

	public Z3nGroupsUserManager(EntityManagerFactory emf) {
		this.emf = emf;
	}

	public void setEntityManagerFactory(EntityManagerFactory emf) {
		this.emf = emf;
	}

	private EntityManager getEntityManager() {
		if (emf == null) {
			throw new RuntimeException(
					"The EntityManagerFactory is null.  This must be passed in to the constructor or set using the setEntityManagerFactory() method.");
		}
		return emf.createEntityManager();
	}

	@Action(Action.ACTION_TYPE.CREATE)
	public String createZ3nGroupsUser(Z3nGroupsUser Z3nGroupsUser)
			throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(Z3nGroupsUser);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.DELETE)
	public String deleteZ3nGroupsUser(Z3nGroupsUser Z3nGroupsUser)
			throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nGroupsUser = em.merge(Z3nGroupsUser);
			em.remove(Z3nGroupsUser);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.UPDATE)
	public String updateZ3nGroupsUser(Z3nGroupsUser Z3nGroupsUser)
			throws Exception {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Z3nGroupsUser = em.merge(Z3nGroupsUser);
			em.getTransaction().commit();
		} catch (Exception ex) {
			try {
				if (em.getTransaction().isActive()) {
					em.getTransaction().rollback();
				}
			} catch (Exception e) {
				ex.printStackTrace();
				throw e;
			}
			throw ex;
		} finally {
			em.close();
		}
		return "";
	}

	@Action(Action.ACTION_TYPE.FIND)
	public Z3nGroupsUser findZ3nGroupsUserByGuId(int guId) {
		Z3nGroupsUser Z3nGroupsUser = null;
		EntityManager em = getEntityManager();
		try {
			Z3nGroupsUser = (Z3nGroupsUser) em.find(Z3nGroupsUser.class, guId);
		} finally {
			em.close();
		}
		return Z3nGroupsUser;
	}

	@Action(Action.ACTION_TYPE.NEW)
	public Z3nGroupsUser getNewZ3nGroupsUser() {
	
		Z3nGroupsUser Z3nGroupsUser = new Z3nGroupsUser();
	
		return Z3nGroupsUser;
	}

	@NamedQueryTarget("getZ3nGroupsUserByZ3nUser")
	public List<Z3nGroupsUser> getZ3nGroupsUserByZ3nUser(Object z3nUser_userId) {
		EntityManager em = getEntityManager();
		List<Z3nGroupsUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroupsUserByZ3nUser");
			query.setParameter("z3nUser_userId", z3nUser_userId);
			results = (List<Z3nGroupsUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nGroupsUserByZ3nGroup")
	public List<Z3nGroupsUser> getZ3nGroupsUserByZ3nGroup(
			Object z3nGroup_groupId) {
		EntityManager em = getEntityManager();
		List<Z3nGroupsUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroupsUserByZ3nGroup");
			query.setParameter("z3nGroup_groupId", z3nGroup_groupId);
			results = (List<Z3nGroupsUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nGroupsUserByGroupLevel")
	public List<Z3nGroupsUser> getZ3nGroupsUserByGroupLevel(int groupLevel) {
		EntityManager em = getEntityManager();
		List<Z3nGroupsUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroupsUserByGroupLevel");
			query.setParameter("groupLevel", groupLevel);
			results = (List<Z3nGroupsUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

	@NamedQueryTarget("getZ3nGroupsUserByGuId")
	public List<Z3nGroupsUser> getZ3nGroupsUserByGuId(int guId) {
		EntityManager em = getEntityManager();
		List<Z3nGroupsUser> results = null;
		try {
			Query query = em.createNamedQuery("getZ3nGroupsUserByGuId");
			query.setParameter("guId", guId);
			results = (List<Z3nGroupsUser>) query.getResultList();
		} finally {
			em.close();
		}
		return results;
	}

}